﻿/*******************************************************************************

*******************************************************************************/
(function (window, undefined) {
    var TwiO = {
        queryString: function (name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        },
        escape: function (str, inNative) {
            var output = "";
            for (var i = 0; i < str.length; i++) {
                var c = str.charAt(i);
                var cc = str.charCodeAt(i);
                if (cc > 0xff) {
                    if (inNative == false) {
                        output += c;
                    }
                    else {
                        output += "\\u" + this.toHex(cc >> 8) + this.toHex(cc & 0xff);
                    }
                }
                else {
                    //数字,大小写字母,以及"+-*/._"不变
                    if (
                          (cc >= 48 && cc <= 57)
                        || (cc >= 64 && cc <= 90)
                        || (cc >= 97 && cc <= 122)
                        || (cc == 42 || cc == 43 || cc == 45 || cc == 46 || cc == 47 || cc == 95)
                        ) {
                        output += c;
                    }
                    else {
                        output += "%" + this.toHex(cc);
                    }
                }
            }
            return output;
        },
        JSonToString: function (json) {
            if (json == null) {
                return "null";
            }
            if (arguments.length > 1) {
                var str = "";
                for (var i = 0; i < arguments.length; i++) {
                    str += "," + this.JSonToString(arguments[i]);
                }
                return str.substring(1);
            }
            var type = typeof json;
            if (type != "object") {
                json = json.toString();
                if (json.match(/^\d+$/g)) {
                    return json;
                }
                return "\"" + encodeURIComponent(json.toString()) + "\"";
            }
            else if (json instanceof Array) {
                var str = "";
                for (var i = 0; i < json.length; i++) {
                    str += "," + this.JSonToString(json[i]);

                }
                if (str != "") {
                    return "[" + str.substring(1) + "]";
                }
                else {
                    return "[]";
                }
            }
            else if ((json instanceof Date) || (json.getYear && json.getMonth && json.getDay && json.getHours && json.getMinutes && json.getSeconds)) {
                return "\"" + encodeURIComponent(json.toLocaleString()) + "\""
            }
            var str = "";
            for (var key in json) {
                var value = json[key];
                if (typeof (value) == "function") {
                    continue;
                }
                //str += ",\"" + key + "\":" +  this.JSonToString(value);
                str += ",\"" + key + "\":\"" + this.JSonToString(value) + "\"";
            }
            if (str != "") {
                return "{" + str.substring(1) + "}";
            }
            else {
                str = json.toString();
                if (str == "[object Object]") {
                    return "{}";
                }
                else {
                    return "\"" + encodeURIComponent(str) + "\"";
                }
            }
        },
        unescape: function (str) {
            return unescape(str);
        },
        toHex: function (n) {
            var nH = (n >> 4) & 0x0f;
            var nL = n & 0x0f;
            return "0123456789ABCDEF".charAt(nH) + "0123456789ABCDEF".charAt(nL);
        },
        toChar: function (str) {
            if (str.substr(0, 2) != "\\u") return str;
            var code = 0;
            for (var i = 2; i < str.length; i++) {
                var cc = str.charCodeAt(i);
                if (cc >= 0x30 && cc <= 0x39)
                    cc = cc - 0x30;
                else if (cc >= 0x41 && cc <= 0x5A)
                    cc = cc - 0x41 + 10;
                else if (cc >= 0x61 && cc <= 0x7A)
                    cc = cc - 0x61 + 10;
                code <<= 4;
                code += cc;
            }
            if (code < 0xff) return str;
            return String.fromCharCode(code);
        },
        form: {//表单处理   
            getFields: function (form) {//获取表单中除disabled外所有控件的名称
                if (!form) { alert("没有指定form对象"); return null; }
                var type = typeof (form);
                if (type != 'string' && type != "object") { alert("没有指定form对象"); return null; }
                if (type == 'string' && form != "") {
                    form = $("#" + form);
                }
                if (!form || form.length == 0) {
                    alert("没有找到form对象");
                    return null;
                }
                var fieldstr = "";
                var fields = $("input:text,input:hidden,input:password,select,textarea", form);
                for (var i = 0; i < fields.length; i++) {
                    if (!fields[i].disabled) {
                        var inputName = fields[i].name;
                        if (inputName != 'uploadify' && inputName != '') {
                            fieldstr += "," + inputName;
                        }
                    }
                }
                var radiofields = $("input:radio", form);
                var radioArr = new Array();
                for (var i = 0; i < radiofields.length; i++) {
                    if (!radiofields[i].disabled) {
                        var inputName = radiofields[i].name;
                        if (inputName != '' && $.inArray(inputName, radioArr) < 0) {
                            radioArr.push(inputName);
                        }
                    }
                }
                for (var i = 0; i < radioArr.length; i++) {
                    var name = radioArr[i];
                    fieldstr += "," + name;
                }
                radioArr = null;

                var checkboxfields = $("input:checkbox", form);
                var checkboxArr = new Array();
                for (var i = 0; i < checkboxfields.length; i++) {
                    if (!checkboxfields[i].disabled) {
                        var inputName = checkboxfields[i].name;
                        if (inputName != '' && $.inArray(inputName, checkboxArr) < 0) {
                            checkboxArr.push(inputName);
                        }
                    }
                }
                for (var i = 0; i < checkboxArr.length; i++) {
                    var name = checkboxArr[i];
                    fieldstr += "," + name;
                }
                checkboxArr = null;

                if (fieldstr != "") {
                    fieldstr = fieldstr.substring(1);
                }
                return fieldstr;
            },
            setValue: function (form, data, isdecode) {//给表单中的控件赋值，form表表单对象或表单区域,可以是该对象，也可是该对象的id， //data为json数据源,isdecode是否解码        
                var type = typeof (form);
                if (type == 'string' && form != "") {
                    form = $("#" + form);
                }
                if (typeof (data) != "object") { alert("数据data应该是json对象"); return; }
                fields = this.getFields(form);
                if (fields && fields != "") {
                    fields = fields.split(",");
                    for (var i = 0; i < fields.length; i++) {
                        var value = eval("data." + fields[i].toLowerCase());
                        if (value) {
                            if (isdecode != false) {
                                value = unescape(value);
                            }
                            var input = $("[name='" + fields[i] + "']", form);
                            if (!input.is(":disabled")) {
                                if (input.is(":radio")) {
                                    for (var j = 0; j < input.length; j++) {
                                        if (input[j].value == value) {
                                            input[j].checked = true;
                                            break;
                                        }
                                    }
                                }
                                else if (input.is(":checkbox")) {
                                    var vs = value.split(',');
                                    for (var j = 0; j < input.length; j++) {
                                        for (var m = 0; m < vs.length; m++) {
                                            if (input[j].value == vs[m]) {
                                                input[j].checked = true; break;
                                            }
                                        }
                                    }
                                }
                                else {
                                    input.val(value);
                                }
                            }
                        }
                    }
                }
            },
            getValue: function (form, isencode, inNative) {//获取表单中控件的值，formid表表单对象id，返回json数据  disabled=true的控件不参与,isencode是否编码

                var type = typeof (form);
                if (type == 'string' && form != "") {
                    form = $("#" + form);
                }
                fields = this.getFields(form);
                var dataStr = {}; //"";
                var optionObject = new Object();
                if (fields && fields != "") {
                    fields = fields.split(",");
                    for (var i = 0; i < fields.length; i++) {
                        var input = $("[name='" + fields[i] + "']", form);
                        var inputValue = "";
                        if (input.is(":radio")) {
                            for (var j = 0; j < input.length; j++) {
                                if (input[j].checked == true) {
                                    inputValue = input[j].value;
                                    break;
                                }
                            }
                        }
                        else if (input.is(":checkbox")) {
                            var str = "";
                            for (var j = 0; j < input.length; j++) {
                                if (input[j].checked == true) {
                                    str += "," + input[j].value;
                                }
                            }
                            if (str != "") {
                                str = str.substring(1);
                            }
                            inputValue = str;
                        }
                        else {
                            inputValue = input.val();
                            inputValue = inputValue.replace(/[ ]/g, "");
                        }
                        if (isencode != false) {
                            inputValue = TwiO.escape(inputValue, inNative);
                        }
                        optionObject[fields[i].toUpperCase()] = inputValue;
                        //dataStr += ",\"" + fields[i].toUpperCase() + "\":\"" + inputValue + "\"";
                    }
                }
                //                if (dataStr != "") {
                //                    dataStr = "{" + dataStr.substring(1) + "}";
                //                }
                return optionObject;
            },
            clearFields: function (form) {//取消输入，清空各字段
                var type = typeof (form);
                if (type == 'string' && form != "") {
                    $("#" + form).find('input').each(function () {
                        var id = $(this).attr('id');
                        $("#" + id).val("");
                    });
                    $("#" + form).find('input[class="easyui-validatebox"]').each(function () {
                        var id = $(this).attr('id');
                        $("#" + id).val("");
                    });
                    $("#" + form).find('input[class="easyui-numberbox"]').each(function () {
                        var id = $(this).attr('id');
                        $("#" + id).val("");
                    });
                    $("#" + form).find('input[class="easyui-combotree"]').each(function () {
                        var id = $(this).attr('id');
                        $("#" + id).combotree('setValue',"");
                    });
                    $("#" + form).find('textarea').each(function () {
                        var id = $(this).attr('id');
                        $("#" + id).val("");
                    });
                    $("#" + form).find('select[class="easyui-combobox"]').each(function () {
                        var id = $(this).attr('id');
                        $("#" + id).combobox('setValue', "");
                    });
                    $("#" + form).find('select[type="checkbox"]').each(function () {
                        var id = $(this).attr('id');
                        $("#" + id).attr("checked", false);
                    });

                }

                //                fields = this.getFields(form);
                //                if (fields && fields != "") {
                //                    fields = fields.split(",");
                //                    for (var i = 0; i < fields.length; i++) {
                //                        var input = $("[name='" + fields[i] + "']", form);
                //                        if (!input.is(":disabled") && !input.is(":hidden")) {
                //                            if (input.is(":radio") || input.is(":checkbox")) {
                //                                for (var j = 0; j < input.length; j++) {
                //                                    input[j].checked = false;
                //                                }
                //                            }
                //                            else {
                //                                input.val("");
                //                            }
                //                        }
                //                    }
                //                }
            },
            validForm: function (form) {
                var flag = true;
                var type = typeof (form);
                if (type == 'string' && form != "") {

                    $('#' + form + '  input').each(function () {

                        if ($(this).attr('required') || $(this).attr('validType')) {
                            if (!$(this).validatebox('isValid')) {
                                flag = false;

                            }
                        }
                    })
                    $('#' + form + '  textarea').each(function () {
                        if ($(this).attr('required') || $(this).attr('validType')) {
                            if (!$(this).validatebox('isValid')) {
                                flag = false;

                            }
                        }
                    })


                    return flag;
                }
                else {
                    alert("没有找到form对象");
                    return null;
                }



            }

        },
        data: {//数据处理           
        }

    };
    window.TwiO = window.js = TwiO;
})(window);

